package com.example.newbook;

import com.example.newbook.controller.SendWeixinController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
@Slf4j
@EnableScheduling
@SpringBootApplication
public class NewbookApplication {

    private String previousContentHash;

    public static void main(String[] args) throws IOException, InterruptedException, NoSuchAlgorithmException {
        SpringApplication.run(NewbookApplication.class, args);
        //NewbookApplication watcher = new NewbookApplication();
        //watcher.init(System.getProperty("user.home") + File.separator +"Desktop"+ File.separator +"redbok.json");
    }

    //public void init(String filePath) throws IOException, InterruptedException, NoSuchAlgorithmException {
    //    // 初始化：获取初始内容的哈希值
    //    previousContentHash = getContentHash(filePath);
    //    String desktopPath = System.getProperty("user.home") + File.separator +"Desktop";
    //
    //    // 设置并启动WatchService
    //    Path path = Paths.get(desktopPath, "redbok.json");
    //    WatchService watchService = FileSystems.getDefault().newWatchService();
    //    path.getParent().register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
    //
    //
    //    // 监控文件修改事件
    //    WatchKey key;
    //    while ((key = watchService.take()) != null) {
    //        for (WatchEvent<?> event : key.pollEvents()) {
    //            if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
    //                continue;
    //            }
    //            Path changedFilePath = (Path) event.context();
    //            if (changedFilePath.endsWith("redbok.json")) {
    //                // 文件被修改，比较内容
    //                String newContentHash = getContentHash(filePath);
    //                if (!newContentHash.equals(previousContentHash)) {
    //                    // 内容有变化，执行操作
    //                    System.out.println("JSON content has been modified.");
    //                    // 更新上一次的内容哈希值
    //                    previousContentHash = newContentHash;
    //                    // 执行你的业务逻辑...
    //                    SendWeixinController sendWeixinController = new SendWeixinController();
    //                    sendWeixinController.sendTemplateMessage(sendWeixinController.readJsonKey(desktopPath, "title"));
    //                } else {
    //                    System.out.println("JSON file was modified but content remains unchanged.");
    //                }
    //            }
    //        }
    //        // 重置WatchKey以接收更多事件
    //        key.reset();
    //    }
    //}

        //private String getContentHash(String filePath) throws IOException, NoSuchAlgorithmException {
        //    // 使用MD5计算文件内容的哈希值
        //    MessageDigest md = MessageDigest.getInstance("MD5");
        //    try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
        //        String line;
        //        while ((line = br.readLine()) != null) {
        //            md.update(line.getBytes(StandardCharsets.UTF_8));
        //        }
        //    }
        //    byte[] digest = md.digest();
        //    return bytesToHex(digest);
        //}
        //
        //private String bytesToHex(byte[] bytes) {
        //    StringBuilder hexString = new StringBuilder();
        //    for (byte b : bytes) {
        //        String hex = Integer.toHexString(0xff & b);
        //        if (hex.length() == 1) hexString.append('0');
        //        hexString.append(hex);
        //    }
        //    return hexString.toString();
        //}

    }